using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace ibba.src.sl.componentes
{

    public partial class ControlMenuBar : UserControl
    {
        public static readonly DependencyProperty BorderRadiusProperty = DependencyProperty.Register("BorderRadius", typeof(CornerRadius), typeof(ControlMenuBar), null);
        public static readonly DependencyProperty VisibilityUserPanelProperty = DependencyProperty.Register("VisibilityUserPanel", typeof(Visibility), typeof(ControlMenuBar), null);
        public static readonly DependencyProperty VisibilityButtonsProperty = DependencyProperty.Register("VisibilityButtons", typeof(Visibility), typeof(ControlMenuBar), null);
        public static readonly DependencyProperty TitleBarProperty = DependencyProperty.Register("TitleBar", typeof(String), typeof(ControlMenuBar), null);
        public static readonly DependencyProperty ButtonIsAnimateProperty = DependencyProperty.Register("ButtonIsAnimate", typeof(bool), typeof(ControlMenuBar), null);
        //public static readonly DependencyProperty BackgroundProperty = DependencyProperty.Register("Background", typeof(Brush), typeof(ControlMenuBar), null);

        public CornerRadius BorderRadius
        {
            get { return (CornerRadius)GetValue(BorderRadiusProperty); }
            set {
                SetValue(BorderRadiusProperty, value);
                brdBarra.CornerRadius = value;
            }
        }

        public Visibility VisibilityUserPanel
        {
            get { return (Visibility)GetValue(VisibilityUserPanelProperty); }
            set
            {
                SetValue(VisibilityUserPanelProperty, value);
                panelUsuario.Visibility = value;
            }
        }

        public Visibility VisibilityButtons
        {
            get { return (Visibility)GetValue(VisibilityButtonsProperty); }
            set
            {
                SetValue(VisibilityButtonsProperty, value);
                botoesMenu.Visibility = value;
            }
        }

        public String TitleBar
        {
            get { return (String)GetValue(TitleBarProperty); }
            set
            {
                SetValue(TitleBarProperty, value);
                txtTitleBar.Text = value;
            }
        }


        public bool ButtonIsAnimate
        {
            get { return (bool)GetValue(ButtonIsAnimateProperty); }
            set
            {
                SetValue(ButtonIsAnimateProperty, value);
            }
        }

        public virtual Brush Background
        {
            get { return brdBarra.Background; }
            set { brdBarra.Background = value; }
        }

        public virtual Brush BorderBrush
        {
            get { return brdBarra.BorderBrush; }
            set { brdBarra.BorderBrush = value; }
        }

        public virtual Thickness BorderThickness
        {
            get { return brdBarra.BorderThickness; }
            set { brdBarra.BorderThickness = value; }
        }
        
        public List<Botao> Botoes { get; set; }
                
        public ControlMenuBar()
        {
            InitializeComponent();

            this.ButtonIsAnimate = true;

            Botoes = new List<Botao>();
            botoesMenu.ItemsSource = Botoes;

            txtUser.Text = string.Empty;
        }

        public string User
        {
            set {txtUser.Text = value;}
        }

        public void AlterarVisibilidadeBotoes(bool pVisivel)
        {
            try
            {
                Botoes.ForEach(b => b.Visivel = pVisivel);
                AtualizarControles();
            }
            catch (Exception)
            { }
        }

        public void AlterarVisibilidadeBotao(string pNome, bool pVisivel)
        {
            try
            {
                Botao b = Botoes.Single(a => a.Nome.Equals(pNome));

                if (b != null)
                {
                    b.Visivel = pVisivel;
                    AtualizarControles();
                }
            }
            catch (Exception)
            {}
                        
        }

        public void AtualizarControles()
        {
            botoesMenu.ItemsSource = Botoes.Where(a => a.Visivel).OrderBy(b => b.Sequencia);
        }

        public void ClearButtons()
        {
            Botoes = new List<Botao>();
            botoesMenu.ItemsSource = Botoes;
        }

        public void AddNewButton(string pNome, string pDescricao, string pNomeImagem, int pSequencia,
                                 ControlMenuBar.Botao.DelegateClick pEvento)
        {
            Botoes.Add(ObterNovoBotao(pNome, pDescricao, pNomeImagem, pSequencia, this.ButtonIsAnimate, pEvento));
        }

        public static ControlMenuBar.Botao ObterNovoBotao(string pNome, string pDescricao, string pNomeImagem, int pSequencia, bool pAnimado,
                                                          ControlMenuBar.Botao.DelegateClick pEvento)
        {

            ControlMenuBar.Botao botao = new ControlMenuBar.Botao()
            {
                Nome = pNome,
                CaminhoImagem = pNomeImagem,
                Descricao = pDescricao,
                Sequencia = pSequencia,
                Animado = pAnimado
            };

            if (pEvento != null)
                botao.EventClick += pEvento;

            return (botao);
        }
        
        private void cbbBotao_Click(object sender, RoutedEventArgs e)
        {
            Botao b = Botoes.Single(a => a.Nome.Equals( ((CustomButtonBar)sender).Tag.ToString() ));
            if (b != null)
            {
                b.Click();
            }
        }

        public class Botao
        {

            public delegate void DelegateClick();
            public event DelegateClick EventClick;

            public Botao()
            {
                this.Visivel = true;
            }

            public void Click()
            {
                if (this.EventClick != null)
                    this.EventClick();

            }

            public string Nome { get; set; }
            public string Descricao { get; set; }
            public string CaminhoImagem { get; set; }
            public int Sequencia { get; set; }
            public bool Visivel { get; set; }
            public bool Animado { get; set; }
        }


    }
}
